package com.david.zhufengfm.adapters;

import android.app.ActionBar;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.david.zhufengfm.R;
import com.david.zhufengfm.entity.discoverrecommend.FocusRecommend;
import com.david.zhufengfm.utils.DimensionUtil;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by David on 2015/10/22.
 * qq:978944820
 */
public class PicPagerAdapter extends PagerAdapter {
    private List<FocusRecommend> picData;

    public PicPagerAdapter(List<FocusRecommend> picData) {
        this.picData = picData;
    }

    @Override
    public int getCount() {
        int ret=0;
        if (picData != null) {
            if (!picData.isEmpty()) {
                ret=Integer.MAX_VALUE;
            }
        }
        return ret;//使用整形最大值，模拟一个假的循环
    }

    /**
     * 判断View是否是通过或者由Object创建出来的
     * 通常对于只返回View的PagerAdapter，那么可以简写成view=object
     * @param view Viewpager 内部管理的View
     * @param object 由instantiateItem()返回的对象
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    /**
     * 根据指定的位置创建一个对象，可以就是View，也可以是管理View的对象
     * 例如Fragment
     * 最终在方法返回之前，一定要将实际的View，添加到container
     * 并且，永远不要调用super
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = container.getContext();
        //TODO 因为getCount返回了整形最大值
        //利用position%数据个数，从而映射成实际数据的索引

        int index=position%picData.size();

        ImageView ret=new ImageView(context);
//        ret.setImageResource(R.mipmap.ic_launcher);
        ViewGroup.LayoutParams lp=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DimensionUtil.dp2px(context,80));
        ret.setLayoutParams(lp);
        Picasso.with(context).load(picData.get(index).getPic()).placeholder(R.mipmap.ic_launcher).into(ret);
        container.addView(ret);
        return ret;
    }

    /**
     * 销毁对象，从容器删除视图
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
